package com.lecture.java8.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;

/**
 * @author luyn
 * @version 1.0
 * @description
 *
 * 一：流的分类
 * 1：按操作单位：字节流（1Byte）、字符流(1Char)
 * 2：按数据流向：输入流、输出流
 *
 * 二：流的体系结构
 *  抽象基类        节点流（或文件流）       缓冲流（处理流的一种）
 *  InputStream    FileInputStream      BufferedInputStream
 *  OutputStream   FileOutputStream     BufferedOutputStream
 *  Reader         FileReader           BufferedReader
 *  Writer         FileWriter           BufferedWrter
 * @date 2021/1/16 10:40
 */
public class FileReaderWriterTest {
    public static void main(String[] args) throws Exception {
        File file1 = new File("E:\\ZN\\1.txt");
        File file = new File("E:\\ZN\\1_2.txt");
        FileReader fileReader = new FileReader(file1);

        //read返回读入的字符，如果达到文件末尾返回-1
        /*int data = fileReader.read();
        while (data != -1) {
                System.out.print((char)data);
            data = fileReader.read();
        }*/

       /* char[] cbuf = new char[7];
        int len;
        while ((len = fileReader.read(cbuf)) != -1) {
            for (int i = 0; i < len; i++) {
                System.out.print(cbuf[i]);
            }
        }*/

        //写出数据到硬盘中
        FileWriter fileWriter = new FileWriter(file, false);
        int len;
        char[] cbuf = new char[3];
        while ((len = fileReader.read(cbuf)) != -1) {
            String str = new String(cbuf, 0, len);
            System.out.println(str);
            fileWriter.write(str);
        }
        //写出操作
        /*fileWriter.write("nihao\n\t");
        fileWriter.write("fsdfsjdlfjsldfj\n");
        fileWriter.write("f3333333333sdfsjdlfjsldfj");*/

        fileReader.close();
        fileWriter.close();
    }
}